<?php
namespace ITSociaal\Themes\Lumber\Hooks;

/**
 * Formatting and tidying hooks
 */
class Formatting implements HooksInterface {

	/**
	 * @param object $actions
	 * @param object $filters
	 *
	 * @return void
	 */
	public function adjustHooks($actions, $filters) {
		remove_action("wp_head", "feed_links", 2);
		remove_action("wp_head", "feed_links_extra", 3);

		/* This could've been simpler, but unfortunately
		 * the hooks framework wasn't meant for multiple
		 * hooks of the same tag ;-;
		 */
		$actions->wp_head2->tag = "wp_head";
		$actions->wp_head2->priority = 2;
		$actions->wp_head3->tag = "wp_head";
		$actions->wp_head3->priority = 3;
	}

	/**
	 * nav_menu_css_class filter handler
	 *
	 * @param array $classes
	 *
	 * @return array
	 */
	public function filterNavMenuCssClass($classes) {
		return array_filter($classes);
	}

	/**
	 * script_loader_tag filter handler
	 *
	 * @param string $tag
	 *
	 * @return string
	 */
	public function filterScriptLoaderTag($tag) {
		return strtr($tag, "'", "\"");
	}

	/**
	 * site_icon_meta_tags filter handler
	 *
	 * @param array $meta_tags
	 *
	 * @return array
	 */
	public function filterSiteIconMetaTags($meta_tags) {
		return array_map([$this, "removeXmlSlash"], $meta_tags);
	}

	/**
	 * style_loader_tag filter handler
	 *
	 * @param string $html
	 *
	 * @return string
	 */
	public function filterStyleLoaderTag($html) {
		return $this->removeXmlSlash(
			strtr(
				$html,
				[
					"='" => "=\"",
					"' " => "\" ",
					" h" => "h",
				]
			)
		);
	}

	/**
	 * wp_head action handler
	 *
	 * @return void
	 */
	public function onWpHead2() {
		ob_start();
		feed_links();
		print $this->removeXmlSlash(ob_get_clean());
	}

	/**
	 * wp_head action handler
	 *
	 * @return void
	 */
	public function onWpHead3() {
		if (apply_filters("feed_links_extra", true)) {
			ob_start();
			feed_links_extra();
			print $this->removeXmlSlash(ob_get_clean());
		}
	}

	/**
	 * Removes " /" from " />"
	 *
	 * @param string $element Markup of single element
	 *
	 * @return string
	 */
	protected function removeXmlSlash($element) {
		return str_replace(" />", ">", $element);
	}

}

/* vi:set ts=4 sw=4 noet: */
